/**  
 * All rights Reserved, Designed By www.woniuxy.com
 * @Title:  Account.java   
 * @Package com.woniu.domain   
 * @Description:    Description  
 * @author: 蜗牛学院  
 * @date:   2019年8月22日 下午6:54:02
 * @user:   Administrator  
 * @version V1.0 
 * @Copyright: 2019 www.woniuxy.com Inc. All rights reserved. 
 * 注意：本内容仅限于成都蜗牛创想科技有限公司，禁止外泄以及用于其他的商业目
 */ 
package com.woniu.ssh.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionContext;
import com.woniu.ssh.domain.Animal;
import com.woniu.ssh.domain.Category;
import com.woniu.ssh.domain.Distribution;
import com.woniu.ssh.service.AnimalService;
import com.woniu.ssh.utils.PageBean;
import com.woniu.ssh.utils.Queryv;

/**
 * @author Administrator
 *
 */
@Controller
public class AnimalAction {
	@Autowired
	AnimalService animalService;		
	private Queryv query;
	private Animal animal;
	private File imgUpload;
	private String imgUploadFileName;
	//private int pageSize;
	public String list() {
//		List<Animal> lists = animalService.findAnimalList();
//		ActionContext.getContext().put("animalList", lists);
		PageBean pb = animalService.findAnimalList(query);
		ActionContext.getContext().put("pb", pb);
		return "list";
	}
	public void showCategory() throws IOException {
		HttpServletResponse resp = ServletActionContext.getResponse();
		resp.setCharacterEncoding("utf-8");
		PrintWriter out = resp.getWriter();
		List<Category> lists = animalService.findAllCategory();
		Gson gson = new Gson();
		String category = gson.toJson(lists);
		out.print(category);
	}
	public void showDistribution() throws IOException {
		HttpServletResponse resp = ServletActionContext.getResponse();
		resp.setCharacterEncoding("utf-8");
		//resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		List<Distribution> lists = animalService.findAllDisttibution();
		Gson gson = new Gson();
		String category = gson.toJson(lists);
		out.print(category);
	}
	public String add() {
		String path="D:\\animal";//保存图片路径
		long d = new Date().getTime();
		String suf = imgUploadFileName.substring(imgUploadFileName.lastIndexOf("."));
		//新的文件名
		String newFileName = d + suf;
		try {
			FileCopyUtils.copy(imgUpload, new File(path+File.separator+newFileName));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		animal.setImgPath(newFileName);
		animalService.insertAnimal(animal);
		return "add";
	}
	public String showAnimal() {
		Animal a = animalService.findAnimalById(animal.getAid());
		ActionContext.getContext().put("a", a);
		return "showAnimal";
	}
	public String update() {
		if(imgUpload != null) {
			String path="D:\\animal";//保存图片路径
			long d = new Date().getTime();
			String suf = imgUploadFileName.substring(imgUploadFileName.lastIndexOf("."));
			//新的文件名
			String newFileName = d + suf;
			try {
				FileCopyUtils.copy(imgUpload, new File(path+File.separator+newFileName));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			animal.setImgPath(newFileName);
		}
		animalService.insertAnimal(animal);
		return "add";
	}
	/**
	 * @return the query
	 */
	public Queryv getQuery() {
		return query;
	}
	/**
	 * @param query the query to set
	 */
	public void setQuery(Queryv query) {
		this.query = query;
	}
	/**
	 * @return the animal
	 */
	public Animal getAnimal() {
		return animal;
	}
	/**
	 * @param animal the animal to set
	 */
	public void setAnimal(Animal animal) {
		this.animal = animal;
	}
	/**
	 * @return the imgUpload
	 */
	public File getImgUpload() {
		return imgUpload;
	}
	/**
	 * @param imgUpload the imgUpload to set
	 */
	public void setImgUpload(File imgUpload) {
		this.imgUpload = imgUpload;
	}
	/**
	 * @return the imgUploadFileName
	 */
	public String getImgUploadFileName() {
		return imgUploadFileName;
	}
	/**
	 * @param imgUploadFileName the imgUploadFileName to set
	 */
	public void setImgUploadFileName(String imgUploadFileName) {
		this.imgUploadFileName = imgUploadFileName;
	}
	
}
